var http = require("http");
var https = require("https");
var url = require("url");
var util = require("util");
var querystring = require("querystring");

function event(req, res) {
    //网址匹配表达式
    let reg = /^(?:(http|https|ftp):\/\/)?((?:[\w-]+\.)+[a-z0-9]+)((?:\/[^/?#]*)+)?(\?[^#]+)?(#.+)?$/i;
    //请求头允许跨域
    res.writeHead(200, {
        "Content-Type": "text/html;charset=UTF-8",
        "Access-Control-Allow-Origin": "*",
        "Access-Control-Allow-Headers": "content-type",
        "Access-Control-Allow-Methods": "DELETE,PUT,POST,GET,OPTIONS",
    });

    let params;

    let get_params = new Promise((resolve, reject) => {
        if (req.method == "GET") {
            params = url.parse(req.url, true).query;
            // console.log(req.url)
            console.log(util.inspect(params));
            for (t in params) {
                if (t!='url'){
                    params.url = params.url+`&${t}=${params[t]}`
                }
            }
            // console.log(params.url)
            resolve(params)
        } else if ((req.method = "POST")) {
            let data = "";
            req.on("data", (d) => {
                data += d;
            });
            req.on("end", () => {
                data = querystring.parse(data);
                params = {
                    url: data.url,
                    cookie: data.cookie
                }
                delete data.url;
                delete data.cookie;
                if (Object.prototype.isPrototypeOf(data)) {
                    params.data = data;
                }
                resolve(params)
            });
        }
    })

    get_params.then(() => {
        if (params && params.url != undefined) {
            if (reg.test(params.url)) {
                let { hostname, pathname, protocol } = new URL(params.url);
                let options = {
                    method: req.method,
                    hostname: hostname,
                    // port: "443",
                    path: pathname,
                    headers: {},
                    rejectUnauthorized: false,
                };
                console.log(params.url)
                let body = [];
                if (protocol.indexOf("https") !== -1) {
                    protocol = https;
                } else {
                    protocol = http;
                }
                if (params.cookie) {
                    options.headers.cookie = params.cookie;
                }
                let new_req = protocol.request(options, (new_res) => {
                    new_res.on("data", (d) => {
                        body.push(d);
                    });
                    new_res.on("end", () => {
                        body_data = Buffer.concat(body);
                        res.write(body_data);
                        res.end();
                    });
                });
                if (params.data) {
                    new_req.write(params.data);
                }
                new_req.end();
                // console.log(new_req.headers);
            } else {
                res.write(`请输入正确的参数值`);
                res.end();
            }
        } else {
            res.write(`请输入正确的参数`);
            res.end();
        }
    })

    // res.end();
}
http.createServer(event).listen(9001);
console.log("运行成功");
